package psnl.bingo.bgtool.model.util;

import cn.hutool.core.io.FileUtil;
import psnl.bingo.bgtool.model.dto.RenameSet;
import psnl.bingo.bgtool.model.dto.RenameSetResp;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileTool {
    public static RenameSetResp renameFilesInDir(RenameSet param) throws Exception{
        RenameSetResp resp = new RenameSetResp();

        File dirFile = new File(param.dirPath);
        if(!dirFile.exists() || !dirFile.isDirectory()) {
            throw new Exception("目录路径不合法");
        }

        String fName;
        String suffix = param.suffix;
        if(param.typeMain == 1) {
            // 按文件名截取
            for (File file : dirFile.listFiles()) {
                fName = file.getName();

                if (param.ifSkip(fName)) {
                    continue;
                }

                if(param.suffix == null) {
                    suffix = fName.contains(".") ? fName.substring(fName.lastIndexOf(".")) : "";
                }

                if(fName.length() > param.start) {
                    if(param.end == null || param.end > fName.length()) {
                        fName = fName.substring(param.start);
                    }else {
                        fName = fName.substring(param.start, param.end);
                    }
                }

                if(null != param.prefix) {
                    fName = param.prefix + fName;
                }

                if(!fName.endsWith(suffix)){
                    fName = fName + suffix;
                }

                System.out.println("old: " + file.getName() + ", new: "+fName);
                if(param.ifTest) {
                    resp.testNewName = fName;
                    break;
                }
                FileUtil.rename(file, fName, false);
            }
        }else if(param.typeMain == 2){
            // 按顺序号命名

            Map<String, File> name2File = new HashMap<>();
            List<File> fileList = new ArrayList<>();

            for (File file : dirFile.listFiles()) {
                if (param.ifSkip(file.getName())) {
                    continue;
                }

                name2File.put(file.getName(), file);
                fileList.add(file);
            }

            if("createTime".equals(param.typeOrder)) {
                // 按创建时间升序
                fileList.sort((a, b)-> (int) (a.lastModified() - b.lastModified()));
            }else if("size".equals(param.typeOrder)) {
                fileList.sort((a, b)-> (int) (a.length() - b.length()));
            }else {
                int start = param.start;
                Integer end = param.end;

                fileList.sort((x, y) -> {
                    String subNameX, subNameY;

                    if(end == null) {
                        subNameX = x.getName().substring(start);
                        subNameY = y.getName().substring(start);
                    }else {
                        if(end > x.getName().length()) {
                            subNameX = x.getName().substring(start);
                        }else {
                            subNameX = x.getName().substring(start, end);
                        }

                        if(end > y.getName().length()) {
                            subNameY = y.getName().substring(start);
                        }else {
                            subNameY = y.getName().substring(start, end);
                        }
                    }

                    if(subNameX.length() == subNameY.length()) {
                        return subNameX.compareTo(subNameY);
                    }else {
                        return subNameX.length() - subNameY.length();
                    }
                });
            }

            int zeroNum = 1;
            if(fileList.size()>9) {
                zeroNum = 2;
            }else if(fileList.size()>99) {
                zeroNum = 3;
            }else if(fileList.size()>999) {
                zeroNum = 4;
            }

            int order = 1;
            for (File file : fileList) {
                if(param.suffix == null) {
                    fName = file.getName();
                    suffix = fName.contains(".") ? fName.substring(fName.lastIndexOf(".")) : "";
                }

                if(zeroNum>1) {
                    fName = String.format("%0"+zeroNum+"d", order);
                }else {
                    fName = order+"";
                }

                if(null != param.prefix) {
                    fName = param.prefix + fName;
                }

                if(!fName.endsWith(suffix)){
                    fName = fName + suffix;
                }

                System.out.println("old: " + file.getName() + ", new: "+fName);
                if(param.ifTest) {
                    resp.testNewName = fName;
                    break;
                }
                FileUtil.rename(file, fName, false);

                order ++;
            }
        }

        return resp;
    }
}
